package com.undsf.chaldea.datacollect

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.undsf.chaldea.entities.ServantMetadata
import com.undsf.crawler.BaseCrawler
import com.undsf.chaldea.datacollect.fgowiki.Servant as FgowikiServantModel

/**
 * Created by Arathi on 2017/8/22.
 */
class FgowikiCrawler : BaseCrawler("fgowiki_pages") {
    val baseUrl = "http://fgowiki.com"
    val objMapper: ObjectMapper = jacksonObjectMapper()
    val redis = httpClient.redis

    override fun crawl() {
        val resource = "/guide/petdetail/"
        val servantIdList = 1..137
        val featureStr: String = "var datadetail = "

        var servantMetaList = ArrayList<ServantMetadata>()

        for (id : Int in servantIdList) {
            val url = baseUrl + resource + id
            val content: String = httpClient.get(url)
            val startAt: Int = content.indexOf(featureStr, 0, false)
            val endAt: Int = content?.indexOf("\n", startAt, false)
            val jsonArrStr: String = content.substring(startAt + featureStr.length, endAt)
            val jsonStr: String = jsonArrStr.substring(1, jsonArrStr.length - 2)
            // println(jsonStr)
            val servantJso: FgowikiServantModel = objMapper.readValue(jsonStr, FgowikiServantModel::class.java)
            val servantMeta: ServantMetadata = servantJso.toServantMetadata()
            // println(servantMeta)
            val jsonMeta = objMapper.writeValueAsString(servantMeta)
            redis.hset("servant_meta", ""+id, jsonMeta)
            servantMetaList.add(servantMeta)
        }

        println(objMapper.writeValueAsString(servantMetaList))
    }
}

fun main(args: Array<String>) {
    val crawler : BaseCrawler = FgowikiCrawler()
    crawler.crawl()
}
